package com.thread;

/**
 * @author :admin
 * @description: TODO edit class Description
 * @create date: 2019/4/3 17:17
 */
public class VolatileDemo implements Runnable{
    private volatile boolean isContinuePrint = true;

    public boolean isContinuePrint() {
        return isContinuePrint;
    }

    public void setContinuePrint(boolean isContinuePrint) {
        this.isContinuePrint = isContinuePrint;
    }

    public void printStringMethod() {
        try {
            while (isContinuePrint == true) {
                System.out.println("run printstringMethod threadName=" + Thread.currentThread().getName());
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) { // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        VolatileDemo target = new VolatileDemo();
        Thread t=new Thread(target);
        t.start();
        System.out.println("我要停止它！ stopThread=" + Thread.currentThread().getName());
        //主线程一直等待while循环执行完，所以这里代码无法执行到
        target.setContinuePrint(false);
    }

    @Override
    public void run() {
        printStringMethod();
    }
}
